package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.po.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class SingerController {
    @Autowired
    private SingerService singerService;

    @RequestMapping(value = "/singer/listSinger",method = RequestMethod.POST)
    public Object getList(){
        JSONObject jsonObject = new JSONObject();
        List<Singer> list = singerService.getList();
        jsonObject.put(Consts.MSG,list);
        return jsonObject;
    }

    @RequestMapping(value = "/singer/selectSinger",method = RequestMethod.POST)
    public Object getSingerById(Singer singer){
        JSONObject jsonObject = new JSONObject();
        singer = singerService.getSingerById(singer);
        jsonObject.put(Consts.MSG,singer);
        return jsonObject;
    }

    @RequestMapping(value = "/singer/selectSingerLikeName",method = RequestMethod.POST)
    public Object getSingerLikeName(Singer singer){
        JSONObject jsonObject = new JSONObject();
        List<Singer> list = singerService.getSingerLikeName(singer);
        jsonObject.put(Consts.MSG,list);
        return jsonObject;
    }

    @RequestMapping(value = "/singer/insert",method = RequestMethod.POST)
    public Object insertSinger(Singer singer){
        JSONObject jsonObject = new JSONObject();
        int flag = singerService.insertSinger(singer);
        if (flag == 1){
            jsonObject.put(Consts.CODE,1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        return jsonObject;
    }
    @RequestMapping(value = "/singer/update",method = RequestMethod.POST)
    public Object updateById(Singer singer){
        JSONObject jsonObject = new JSONObject();
        int flag = singerService.updateById(singer);
        if (flag != 0){
            jsonObject.put(Consts.CODE,1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        return jsonObject;
    }
    @RequestMapping(value = "/singer/delete",method = RequestMethod.POST)
    public Object deleteById(Singer singer){
        JSONObject jsonObject = new JSONObject();
        int flag = singerService.deleteById(singer);
        if (flag == 1){
            jsonObject.put(Consts.CODE,1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        return jsonObject;
    }
    @RequestMapping(value = "/singer/updateSingerPic",method = RequestMethod.POST)
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if(avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img" +System.getProperty("file.separator")+"singerPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdirs();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/singerPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            int flag = singerService.updateById(singer);
            if(flag > 0){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"上传成功");
                jsonObject.put("pic",storeAvatorPath);
                return jsonObject;
            }
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败");
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }
}
